原始データをラベルのないストリーム、メモリの荒野に例えます。Rustでは、データをまず「連続したチャンク」として扱い始めます。 連続したチャンク (スライスと配列)。この原始データから定義済みのスキーマへの移行は、匿名のメモリから 目的を持った構造。
1. 「原始」層
スライスと配列は、データの最もシンプルな形を表します。安全は コンパイル時所有権チェック ランタイムのオーバーヘッドではなく、&によって、値を移動せずにデータに対する「ビュー」を作成できます。
2. 意味的限界
関数が first_word のように柔軟ですが( String、 &str、またはリテラルを受け入れる)、しかし意味的な限界に達します。コンパイラはメモリが安全であることは把握していますが、データが 何を表すか (例:ユーザー名とセンサー読み取り値など)は、 Struct。
アーキテクチャの原則: 所有権、借用、スライスという概念により、Rustプログラムはコンパイル時にメモリ安全性が保証され、ガベージコレクタの必要性がありません。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the main benefit of using a string slice (&str) over a String in a function signature?
It takes ownership of the data to prevent leaks.
It allows the function to accept both Strings and string literals via deref coercion.
It automatically converts the text to uppercase.
It stores the data on the stack instead of the heap.
✅ Correct!
Correct! Using &str makes the API more flexible as it can view data owned by Strings or literals.❌ Incorrect
Slices do not take ownership; they are borrowed views.QUESTION 2
In the expression 'let slice = &a[1..3]', what indices of array 'a' are included?
1, 2, and 3
1 and 2
2 and 3
Only index 1
✅ Correct!
Rust range syntax [n..m] is inclusive of the start and exclusive of the end.❌ Incorrect
The range [1..3] includes index 1 and 2, but excludes 3.QUESTION 3
Why do we say raw slices lack 'domain clarity'?
They are technically unsafe to use in production.
They don't provide metadata about what the data represents in the business logic.
They cannot be used with integers.
They are limited to 256 bytes of data.
✅ Correct!
Correct. A &[u8] could be a file, an image, or a name; only a Struct provides the semantic name.❌ Incorrect
Slices are memory-safe, but they are 'anonymous' chunks of data.QUESTION 4
Does a slice (&[i32]) own the data it points to?
Yes, it takes ownership of the range.
No, it is a reference to a portion of data owned by another variable.
Only if the array is mutable.
Yes, but only at compile time.
✅ Correct!
Slices are inherently borrows. They must not outlive the data they point to.❌ Incorrect
If a slice owned data, it would be a Vector or an Array, not a slice.QUESTION 5
When does Rust check for slice out-of-bounds errors?
Exclusively at compile time.
Exclusively at runtime.
Compile time for fixed ranges; runtime for dynamic indices.
Rust does not check for bounds errors.
✅ Correct!
Rust uses a mix of compile-time analysis and runtime checks to ensure memory safety.❌ Incorrect
Rust is famous for preventing buffer overflows through strict checking.Case Study: Sensor Data Streaming
Transitioning from Raw Slices to Domain Structs
You are processing a stream of temperature readings from an IoT device. Initially, you handle the data as a raw slice `&[f64]`. You need to ensure the logic is safe and then refactor for clarity.
Q
1. Why might using `&[f64]` lead to bugs in a large team project compared to a named Struct?
Solution:
A raw slice like `&[f64]` lacks context. Without a schema, a developer might confuse a slice of 'temperatures' with a slice of 'humidity' or 'timestamps,' as both share the same underlying type.
A raw slice like `&[f64]` lacks context. Without a schema, a developer might confuse a slice of 'temperatures' with a slice of 'humidity' or 'timestamps,' as both share the same underlying type.
Q
2. If you create a slice `let window = &readings[0..10]`, what happens if the `readings` vector is cleared?
Solution:
The Rust compiler will throw an error. The slice `window` borrows `readings`, so `readings` cannot be modified (cleared) while the slice is still in use, preventing a dangling pointer.
The Rust compiler will throw an error. The slice `window` borrows `readings`, so `readings` cannot be modified (cleared) while the slice is still in use, preventing a dangling pointer.
Q
3. How does the 'first_word' pattern apply to this sensor data?
Solution:
Just as `first_word` identifies a delimiter in a string, you could write a function that finds the first 'out-of-range' reading in a slice. It processes raw data flexibly without needing to own the entire dataset.
Just as `first_word` identifies a delimiter in a string, you could write a function that finds the first 'out-of-range' reading in a slice. It processes raw data flexibly without needing to own the entire dataset.